﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Engine.Classes.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import com.greensock.*;
    import Classes.actions.*;
    import Engine.*;
    import com.zynga.skelly.util.*;

    public class MerchantCrowdManager {

        protected static const MAX_BEELINE_DISTANCE:Number = 4;
        protected static const V_UP:Vector3 = new Vector3(0, 0, 1);

        protected var m_maxNpcWaveRadius:int = 0;
        protected var m_secondsSinceLastProduction:Number = 0;
        protected var m_secondsTillNextConsumption:Number = 0;
        protected var m_wave:ConsumerShockwave = null;
        protected var m_preview:DisplayObject = null;
        protected var m_merchant:IMerchant = null;
        protected var m_npcsCaptured:int = 0;
        protected var m_peepsRemainingToSpawn:int = 0;
        protected var m_peepConsumeEnabled:Boolean = false;
        protected var m_maxNpcsCaptured:int = 0;
        protected var m_peepSpawnDelaySec:Number = 2147483647;
        protected var m_peepConsumeDelaySec:Number = 2147483647;

        public function MerchantCrowdManager(_arg1:IMerchant, _arg2:String){
            this.m_merchant = _arg1;
            this.loadShockWaveSettings(_arg2);
            this.m_peepSpawnDelaySec = Global.gameSettings.getInt("npcSpawnTimeSec", 1);
            this.m_peepConsumeDelaySec = Global.gameSettings.getInt("npcConsumeTimeSec", 5);
        }
        public function loadShockWaveSettings(_arg1:String):void{
            var _local3:XMLList;
            var _local2:Item = Global.gameSettings.getItemByName(_arg1);
            if (((_local2) && (_local2.shockwaveXml.length()))){
                _local3 = _local2.shockwaveXml;
                this.m_maxNpcWaveRadius = int(_local2.shockwaveXml.@radius);
                this.m_maxNpcsCaptured = int(_local2.shockwaveXml.@captured);
            };
        }
        public function get npcsCaptured():int{
            return (this.m_npcsCaptured);
        }
        public function get npcsWaitingToSpawn():int{
            return (this.m_peepsRemainingToSpawn);
        }
        public function cleanup():void{
            this.m_merchant = null;
            this.m_peepsRemainingToSpawn = 0;
            if (this.m_wave){
                this.m_wave.stop();
                this.m_wave = null;
            };
            if (((this.m_preview) && (this.m_preview.parent))){
                this.m_preview.parent.removeChild(this.m_preview);
                this.m_preview = null;
            };
        }
        public function startSpawningPeeps(_arg1:uint):void{
            this.m_peepsRemainingToSpawn = (this.m_peepsRemainingToSpawn + _arg1);
        }
        public function onUpdate(_arg1:Number):void{
            this.m_secondsSinceLastProduction = (this.m_secondsSinceLastProduction + _arg1);
            if ((((this.m_secondsSinceLastProduction > this.m_peepSpawnDelaySec)) && ((this.m_peepsRemainingToSpawn > 0)))){
                this.spawnPeeps();
            };
            this.m_secondsTillNextConsumption = (this.m_secondsTillNextConsumption - _arg1);
            if (this.m_secondsTillNextConsumption <= 0){
                if (this.m_peepConsumeEnabled){
                    this.consumePeeps(1);
                } else {
                    this.m_secondsTillNextConsumption = 1;
                };
            };
        }
        public function startWavePreview():void{
            var _local1:Number;
            var _local2:Vector3;
            var _local3:Vector3;
            var _local4:Vector3;
            var _local5:Point;
            var _local6:Sprite;
            if (!this.m_preview){
                this.m_preview = new EmbeddedArt.businessShockwavePreview();
                _local1 = ((Constants.TILE_WIDTH * ConsumerShockwave.ORTHO_FACTOR) / 200);
                this.m_preview.scaleX = (this.m_preview.scaleY = (_local1 * this.m_maxNpcWaveRadius));
                _local2 = this.m_merchant.getSize();
                _local3 = this.m_merchant.getPosition();
                _local4 = new Vector3((_local3.x + (_local2.x / 2)), (_local3.y + (_local2.y / 2)));
                _local5 = IsoMath.tilePosToPixelPos(_local4.x, _local4.y);
                this.m_preview.x = _local5.x;
                this.m_preview.y = _local5.y;
                _local6 = (Global.world.getObjectLayerByName("road").getDisplayObject() as Sprite);
                _local6.addChild(this.m_preview);
                this.m_preview.transform.colorTransform.color = 0xFFFFFF;
                TweenLite.from(this.m_preview, 0.2, {
                    scaleX:0,
                    scaleY:0
                });
            };
        }
        public function endWavePreview():void{
            if (this.m_preview){
                TweenLite.to(this.m_preview, 0.3, {
                    alpha:0,
                    onComplete:Curry.curry(function (_arg1:DisplayObject):void{
                        if (((_arg1) && (_arg1.parent))){
                            _arg1.parent.removeChild(_arg1);
                        };
                    }, this.m_preview)
                });
                this.m_preview = null;
            };
        }
        public function startCollecting():void{
            this.m_peepConsumeEnabled = true;
            this.makeNpcCrowdCheer();
        }
        public function stopCollecting():void{
            this.m_peepConsumeEnabled = false;
        }
        public function performShockwave():void{
            var _local1:MapResource = this.m_merchant.getMapResource();
            var _local2:Number = Global.gameSettings.getNumber("bizWaveVelocity", 1);
            this.m_wave = new ConsumerShockwave(_local1, _local2, this.m_maxNpcWaveRadius, this.onNpcConsumed, this.onWaveFinished);
            this.m_wave.start();
        }
        public function performFranchiseShockwave():void{
            var _local1:MapResource = this.m_merchant.getMapResource();
            var _local2:Number = Global.gameSettings.getNumber("bizWaveVelocity", 1);
            this.m_wave = new FranchiseShockwave(_local1, _local2, this.m_maxNpcWaveRadius, this.onNpcConsumedNoEnter, this.onWaveFinished);
            this.m_wave.start();
        }
        public function findNpcsInRange():Array{
            var center:* = null;
            var size:* = this.m_merchant.getSize();
            var corner:* = this.m_merchant.getPosition();
            center = new Vector3((corner.x + (size.x / 2)), (corner.y + (size.y / 2)));
            var predicate:* = function (_arg1:WorldObject):Boolean{
                var _local2:NPC = (_arg1 as NPC);
                return (((_local2) && ((_local2.getPosition().subtract(center).length() <= m_maxNpcWaveRadius))));
            };
            return (Global.world.getObjectsByPredicate(predicate));
        }
        protected function spawnPeeps():void{
            this.m_peepsRemainingToSpawn--;
            this.m_secondsSinceLastProduction = 0;
            Global.world.citySim.npcManager.createBusinessPeepWalkers(1);
        }
        protected function consumePeeps(_arg1:int):void{
            var _local5:Peep;
            var _local2:Vector3 = this.m_merchant.getHotspot();
            var _local3:Number = Math.min(this.m_merchant.getSize().x, this.m_merchant.getSize().y);
            var _local4:Array = this.findPeepsByLocation(_local2, _local3, _arg1);
            for each (_local5 in _local4) {
                if (((!(_local5.isFranchiseFreebie)) && (_local5.isFreeAgent))){
                    this.makeNpcEnterMerchant(_local5);
                };
            };
            this.m_secondsTillNextConsumption = this.m_peepConsumeDelaySec;
            if ((_local4.length == 0)){
                this.m_secondsTillNextConsumption = Math.random();
            };
        }
        protected function findPeepsByLocation(_arg1:Vector3, _arg2:Number, _arg3:int=2147483647):Array{
            var peep:* = null;
            var wrap:* = null;
            var distance:* = NaN;
            var point:* = _arg1;
            var radius:* = _arg2;
            var limit:Number = _arg3;
            var predicate:* = function (_arg1:Peep):Boolean{
                return (((_arg1.isFreeAgent) && ((_arg1.getPosition().subtract(point).length() <= radius))));
            };
            var peeps:* = Global.world.citySim.npcManager.findAllPeepsByPredicate(predicate);
            var wraps:* = new Array();
            for each (peep in peeps) {
                distance = peep.getPosition().subtract(point).length();
                wraps.push({
                    distance:distance,
                    peep:peep
                });
            };
            wraps.sortOn("distance");
            if (wraps.length > limit){
                wraps.splice(limit);
            };
            peeps = new Array();
            for each (wrap in wraps) {
                peeps.push(wrap.peep);
            };
            return (peeps);
        }
        protected function makeNpcCrowdCheer():void{
            var _local2:Peep;
            var _local1:Array = this.findPeepsByLocation(this.m_merchant.getHotspot(), 6);
            for each (_local2 in _local1) {
                if (((_local2.isFreeAgent) && (!(_local2.isFranchiseFreebie)))){
                    _local2.getStateMachine().removeAllStates();
                    _local2.getStateMachine().addActions(new ActionEnableFreedom(_local2, false), new ActionPlayAnimation(_local2, "jump", 0.7), new ActionEnableFreedom(_local2, true));
                };
            };
        }
        public function makeNpcEnterMerchant(_arg1:NPC):void{
            var peep:* = null;
            var npc:* = _arg1;
            var isNotPeep:* = !((npc is Peep));
            peep = (npc as Peep);
            if (((peep) && (peep.isFranchiseFreebie))){
                return;
            };
            var hotspot:* = this.m_merchant.getHotspot();
            var distance:* = npc.getPosition().subtract(hotspot).length();
            var beeline:* = (distance < MAX_BEELINE_DISTANCE);
            var time:* = Global.gameSettings.getInt("npcVisitTimeSec", 1);
            npc.getStateMachine().removeAllStates();
            npc.getStateMachine().addActions(new ActionEnableFreedom(npc, false));
            if (npc.getItemName() == "NPC_mayor"){
                npc.getStateMachine().addActions(new ActionPlayAnimation(npc, "cheer", 3));
            } else {
                if (beeline){
                    npc.getStateMachine().addActions(new ActionNavigateHotspots(npc, this.m_merchant.getMapResource()));
                } else {
                    npc.getStateMachine().addActions(new ActionNavigate(npc, this.m_merchant.getMapResource(), null).setPathType(RoadManager.PATH_FULL));
                };
            };
            if (isNotPeep){
                npc.getStateMachine().addActions(new ActionDie(npc));
                return;
            };
            npc.getStateMachine().addActions(new ActionFn(npc, function ():void{
                if (!Global.isVisiting()){
                    m_merchant.performVisit(peep);
                    m_merchant.performVisitAnimation(peep);
                };
            }), new ActionTween(npc, ActionTween.TO, 0.5, {alpha:0}), new ActionPause(npc, time), new ActionFn(npc, function ():void{
                npc.animation = "static";
            }));
            if (peep.spawnSource == Peep.SOURCE_TOURBUS){
                npc.getStateMachine().addActions(new ActionDie(npc));
                return;
            };
            if (!this.m_merchant.isAttached()){
                npc.getStateMachine().removeAllStates();
                npc.getStateMachine().addActions(new ActionPlayAnimation(npc, "idle", 10), new ActionPlayAnimation(npc, "wave", 5), new ActionDie(npc));
                return;
            };
            npc.getStateMachine().addActions(new ActionTween(npc, ActionTween.TO, 0.5, {alpha:1}), new ActionEnableFreedom(npc, isNotPeep), new ActionFn(npc, function ():void{
                if (peep){
                    peep.lastMerchantId = m_merchant.getId();
                };
                npc.showFeedbackBubble((new EmbeddedArt.happy() as DisplayObject), 5);
            }));
        }
        protected function onNpcConsumed(_arg1:Array):void{
            var _local2:NPC;
            for each (_local2 in _arg1) {
                if (this.m_npcsCaptured < this.m_maxNpcsCaptured){
                    this.makeNpcEnterMerchant(_local2);
                    this.m_npcsCaptured++;
                };
            };
        }
        protected function onNpcConsumedNoEnter(_arg1:Array):void{
            this.m_npcsCaptured = ((this.m_npcsCaptured + _arg1.length) % this.m_maxNpcsCaptured);
        }
        protected function onWaveFinished():void{
            this.m_wave.stop();
            this.m_wave = null;
            this.m_merchant.onWaveFinished();
            this.m_npcsCaptured = 0;
        }

    }
}//package Classes.sim 
